﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class admin_detailDeposit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
         //permiso();

        if (!Page.IsPostBack)
        {

            int id = 0;
            ArrayList array = new ArrayList();

            DataSet ds = DB.getBanks();

            llenarcb_bank(ds);
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            if (id != 0)
            {// si viene a ver un deposito en especial
                array.Add(id.ToString());
                DataSet dsdep = DB.getDeposits(4, array);
                if (dsdep.Tables[0].Rows.Count > 0)
                {
                    tb_iddep.Text = dsdep.Tables[0].Rows[0][0].ToString();
                    tb_reference1.Text = dsdep.Tables[0].Rows[0][3].ToString();
                    tb_date1.Text = dsdep.Tables[0].Rows[0][4].ToString();
                    tb_amount.Text = dsdep.Tables[0].Rows[0][5].ToString();
                    cb_bank1.SelectedValue = dsdep.Tables[0].Rows[0][1].ToString();
                    lb_total.Visible = true;
                    tb_total.Visible = true;
                }
                DataSet dsorder = DB.GetCheckbyDep(id.ToString());
                if (dsorder != null)
                {
                    gv_deposit.DataSource = dsorder;
                    gv_deposit.DataBind();
                    int cont2 = dsorder.Tables[0].Rows.Count - 1;
                    if (cont2 >= 0)
                    {
                        
                        string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][9].ToString()));
                        tb_total.Text = tst;
                    }
                    else { tb_total.Text = "0"; }
                }
                btn_addcheck.Visible = true;
                bt_finish.Visible = true;
                btn_edit.Visible = true;
                enableControls(false);
                bt_save.Visible = false;

                

            }
            else
            {
                enableControls(true);
                bt_finish.Visible = true;
                btn_addcheck.Visible = false;
                bt_save.Visible = true;
                tb_iddep.Text = "0";
                lb_total.Visible = false;
                tb_total.Visible = false;
            }


        }
        else {
            int id = 0;
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            DataSet dsorder = DB.GetCheckbyDep(id.ToString());
            if (dsorder != null)
            {
                int cont2 = dsorder.Tables[0].Rows.Count - 1;
                if (cont2 >= 0)
                {

                    string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][9].ToString()));
                    tb_total.Text = tst;
                }
                else {
                    tb_total.Text = "0";
                }
                //DataSet dsdeposits = DB.getDeposits(5, null);
                gv_deposit.DataSource = dsorder;
                gv_deposit.DataBind();
            }
        
        }
        int comprobar= comprobarsum();
       

        }

private int comprobarsum()
{
    int result = 0;
    // en cualquier caso que pinte color sumatoria
    if (!tb_total.Text.Equals("") && (!tb_amount.Text.Equals("")))
    {
        double temp = double.Parse(tb_total.Text);
        double ammoun = double.Parse(tb_amount.Text);
        if (temp < ammoun)
        {
            tb_total.ForeColor = System.Drawing.Color.OrangeRed;
            //tb_total.BackColor = System.Drawing.Color.OrangeRed;
            result = 0;
            lb_msg.Text = "The sum of the checks is less than the amount of deposit";
        }
        else if (temp > ammoun)
        {
            tb_total.ForeColor = System.Drawing.Color.Red;
            //tb_total.BackColor = System.Drawing.Color.Blue;
            result = 2;
            lb_msg.Text = "The sum of the checks is greater than the amount of deposit"; 
        }
        else
        {
            tb_total.ForeColor = System.Drawing.Color.DarkGreen;
            //tb_total.BackColor = System.Drawing.Color.Red;
            result = 1;
            lb_msg.Text = "Exit?"; 
        }
        

}
    return result;
    }
    private void llenarcb_bank(DataSet ds)
    {
        if (ds != null)
        {
            ListItem li = new ListItem();
            li.Text = "Select One";
            li.Value = "0";
            cb_bank1.Items.Add(li);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i][2].ToString() == "1")
                {
                    li = new ListItem();
                    li.Value = ds.Tables[0].Rows[i][0].ToString();
                    li.Text = ds.Tables[0].Rows[i][1].ToString();
                    cb_bank1.Items.Add(li);
                }
            }
        }
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        RE_GenericBean re= new RE_GenericBean();
        re.intC1= int.Parse( cb_bank1.SelectedValue);
        re.strC1= tb_reference1.Text;
        re.strC3=  tb_amount.Text;
        re.strC2= tb_date1.Text;

        int resp = DB.addDeposit(re,0);
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        RE_GenericBean re = new RE_GenericBean();
        re.intC1 = int.Parse(cb_bank1.SelectedValue);
        re.strC1 = tb_reference1.Text;
        re.strC3 = tb_amount.Text;
        re.strC2 = tb_date1.Text;
        int id = 0;
        if (!tb_iddep.Text.Equals("")) id= int.Parse(tb_iddep.Text);

        int resp = DB.addDeposit(re,id);
        if (resp != 0)
        {
            tb_iddep.Text = resp.ToString();
            btn_addcheck.Visible = true;
            btn_addcheck.Enabled = true;
            bt_save.Visible = false;
            btn_edit.Visible = true;
            Response.Redirect("detailCheck.aspx?dep="+resp.ToString());

        }
        else {

            Response.Write("<script> alert('Deposit not saved ');</script>");
        }
      
        DataSet dsorder = DB.GetCheckbyDep(id.ToString());
        if (dsorder != null)
        {
            int cont2 = dsorder.Tables[0].Rows.Count - 1;
            if (cont2 >= 0)
            {
                string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][9].ToString()));
                tb_total.Text =tst;
            }
            else {
                tb_total.Text = "0";
            }
            gv_deposit.DataSource = dsorder;
            gv_deposit.DataBind();
        }
    }
    protected void bt_finish_Click(object sender, EventArgs e)
    {
        int comprobar = comprobarsum();

     
        if (comprobar == 0) { bt_finish_ModalPopupExtender.Show(); }
        if (comprobar == 1) { Response.Redirect("Accounting.aspx"); }
        if (comprobar == 2) { bt_finish_ModalPopupExtender.Show(); }
          
                
       

        
    }
    protected void gv_deposit_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
             e.Row.Cells[1].Style.Add("display", "none");//ordernum
             e.Row.Cells[10].Style.Add("display", "none");//SUM
         
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Edit";
            e.Row.Cells[2].Text = "Bank";
            e.Row.Cells[3].Text = "Reference";
            e.Row.Cells[4].Text = "Amount";
            e.Row.Cells[5].Text = "Client";
            e.Row.Cells[6].Text = "Company";
            e.Row.Cells[7].Text = "Account";
            e.Row.Cells[8].Text = "Date";
            e.Row.Cells[9].Text = "Date Processed";
            e.Row.Cells[10].Style.Add("display", "none");//statusname
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int id = 0;
        if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
        if (id != 0)
        {
            Session["newCheck"] = 1;
            Response.Redirect("detailCheck.aspx?dep="+id.ToString());
        }
        else {
 
            Response.Write("<script language='javascript'>alert('First select a deposit in order to submit a new check');<script>");
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        enableControls(true);
        btn_edit.Visible = false;
        bt_save.Visible = false;
        btn_addcheck.Visible = false;
        lnk_update.Visible = true;
    }

    private void enableControls(bool p)
    {
        cb_bank1.Enabled = p;
        tb_amount.Enabled = p;
        tb_date1.Enabled = p;
        tb_reference1.Enabled = p;
        bt_save.Enabled = p;
        

    }

    protected void lnk_msg_ok0_Click(object sender, EventArgs e)
    {
        Response.Redirect("Accounting.aspx");
    }
    protected void lnk_update_Click(object sender, EventArgs e)
    {

        RE_GenericBean re = new RE_GenericBean();
        re.intC1 = int.Parse(cb_bank1.SelectedValue);
        re.strC1 = tb_reference1.Text;
        re.strC3 = tb_amount.Text;
        re.strC2 = tb_date1.Text;
        int id = 0;
        if (!tb_iddep.Text.Equals("")) id = int.Parse(tb_iddep.Text);

        int resp = DB.addDeposit(re, id);
        if (resp != 0)
        {
            tb_iddep.Text = resp.ToString();
            btn_addcheck.Visible = true;
            btn_addcheck.Enabled = true;
            bt_save.Visible = false;
            btn_edit.Visible = true;
            lnk_update.Visible = false;


        }
        else
        {

            Response.Write("<script> alert('Deposit not saved ');</script>");
        }

        DataSet dsorder = DB.GetCheckbyDep(id.ToString());
        if (dsorder != null)
        {
            int cont2 = dsorder.Tables[0].Rows.Count - 1;
            if (cont2 >= 0)
            {
                string tst = String.Format("{0:0.00}", double.Parse(dsorder.Tables[0].Rows[cont2][9].ToString()));
                tb_total.Text = tst;
            }
            else
            {
                tb_total.Text = "0";
            }
            gv_deposit.DataSource = dsorder;
            gv_deposit.DataBind();
        }
    }
    protected void lnk_msg_ok_Click(object sender, EventArgs e)
    {

    }
}